package org.catj.app;

import org.catj.entity.Dwarf;
import org.catj.util.JobInfo;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import sun.org.mozilla.javascript.internal.ast.WithStatement;

import javax.swing.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class DwarfRecruiterMain {
    public static void main(String[] args) {
        /* JFileChooser chooser = new JFileChooser();
        int retVal = chooser.showOpenDialog(null);
        if(retVal == JFileChooser.APPROVE_OPTION){
            File xmlFile = chooser.getSelectedFile();
            parseXML(xmlFile);
        }*/
        parseXML(new File("C:\\Dwarves.xml"));
    }

    private static void parseXML(File xmlFile) {
        DwarfRecruiter recruiter = new DwarfRecruiter();
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("Creature");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) node;
                    recruiter.parseDwarf(element);
                }
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        for(Dwarf dwarf : recruiter.getDwarves()){
            Object[] jobs = dwarf.getSortedJobScores().values().toArray();
            TreeSet bestJobs = (TreeSet) jobs[jobs.length - 1];
            if(bestJobs.size() > 1){
                System.out.println("The best jobs for " + dwarf.getName() + " are "
                    + jobs[jobs.length - 1] + " with a score of " + dwarf.getJobScore((String) bestJobs.first()));
            }
            else{
                System.out.println("The best job for " + dwarf.getName() + " is "
                    + jobs[jobs.length - 1]+ " with a score of " + dwarf.getJobScore((String) bestJobs.first()));
            }
        }

        for(String jobName : JobInfo.ALL_JOBS){
            System.out.println();
            System.out.print("Best " + jobName + "s in order : \n");
            TreeMap<Float, Dwarf> orderedDwarves = (TreeMap<Float, Dwarf>) recruiter.getSortedDwarvesForJob(jobName);
            int count = 0;
            for(Float score : orderedDwarves.descendingKeySet()){
                Dwarf dwarf = orderedDwarves.get(score);
                System.out.println("\t" + dwarf.getName() + " - " + score);
                count++;
                if(count >= 5){
                    break;
                }
            }
        }
        int j = 5;
    }
}
